/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package calculator;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author eleven
 */
public class CalculatorFactory {

    private Map typesCalculators = defaultMap();
    private String typeCalculator = null;

    public CalculatorFactory(String typeCalculator) {
        this.typeCalculator = typeCalculator;
    }

    public Calculator getCalculator() {
        Class classCalculator = (Class) typesCalculators.get(this.typeCalculator);
        if (classCalculator != null) {
            try {
                return (Calculator) (classCalculator.newInstance());
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
        return null;
    }

    private Map defaultMap() {
        Map map = new HashMap();
        map.put(new CrowflightCalculator().getType().getId(), CrowflightCalculator.class);
        map.put(new DistanceTableCalculator().getType().getId(), DistanceTableCalculator.class);
        map.put(new ZeroCalculator().getType().getId(), ZeroCalculator.class);
        return map;
    }
}
